package com.fxsh168.server.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.fxsh168.server.R;
import com.fxsh168.server.adapters.MyTwoAdapter;
import com.fxsh168.server.base.BaseActivity;
import com.fxsh168.server.entitys.MyTwoEntity;
import com.fxsh168.server.interfaces.Constant;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.List;

import okhttp3.Call;


public class MyTwoFenActivity extends BaseActivity implements AdapterView.OnItemClickListener {
    private String token, mPhone;
    MyTwoAdapter myTwoAdapter;
    ListView listTwo;
    List<String> data;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_two);
        listTwo = (ListView) findViewById(R.id.listTwo);
        SharedPreferences sp = getSharedPreferences(Constant.LOGINCLASS, MODE_PRIVATE);
        mPhone = sp.getString("mPhone", "");
        token = sp.getString("token", "");
        listTwo.setOnItemClickListener(this);
        getMytwo();
    }

    private void getMytwo() {
        OkHttpUtils.post().addParams("phone", mPhone).addParams("type", "2").url(Constant.MYTWO).build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e("tag", "我的二级失败的原因" + e);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("tag", "我的二级数据" + response);
                        MyTwoEntity myTwoEntity = new Gson().fromJson(response, MyTwoEntity.class);
                        String code = myTwoEntity.getCode();
                        if (code.equals("200")) {
                            data = myTwoEntity.getData();
                            myTwoAdapter = new MyTwoAdapter(data, MyTwoFenActivity.this);
                            listTwo.setAdapter(myTwoAdapter);
                        }
                    }
                });
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Intent intent = new Intent(MyTwoFenActivity.this, MyYaoThreeActivity.class);
        String s = data.get(i);
        intent.putExtra("iid", s);
        startActivity(intent);
    }
}
